import sys
from pathlib import Path, PurePath


# ----------- find root path
def find_root() -> PurePath:
    anchor = '__root__.py'
    script_path = PurePath(__file__)
    parents = script_path.parents

    def _recurse_find(dep: int) -> PurePath:
        dir_n = PurePath()
        if dep < len(parents):
            dir_n = parents[dep]
            cwd = Path().joinpath(dir_n, anchor)
            if cwd.exists():
                sys.path.insert(0, dir_n.as_posix())
                print(f'The base directory is: {dir_n}')
            else:
                _recurse_find(dep + 1)
        else:
            raise RuntimeError(f"I can't find any {anchor} in this project")
        return dir_n

    return _recurse_find(0)
